package com.sky.common.basic;

import com.github.pagehelper.PageInfo;
import com.sky.common.enums.SysEnum;

public class Results<T> {


    /**
     * 状态码
     **/
    private Integer code;

    /**
     * 返回消息
     **/
    private String msg;

    /**
     * 返回消息，兼容后续新改成的 message
     */
    private String message;

    /**
     * 返回数据
     **/
    private T data;


    public Results() {
    }

    public Results(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.message = msg;
    }

    public Results(SysEnum sysEnum) {
        this.code = sysEnum.getCode();
        this.msg = sysEnum.getMsg();
        this.message = sysEnum.getMsg();
    }

    public Results(SysEnum sysEnum, T data) {
        this.code = sysEnum.getCode();
        this.msg = sysEnum.getMsg();
        this.message = sysEnum.getMsg();
        this.data = data;
    }

    public Results(SysEnum sysEnum, String msg) {
        this.code = sysEnum.getCode();
        this.msg = msg;
        this.message = msg;
    }

    public Results(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.message = msg;
        this.data = data;
    }

    public Results results(SysEnum sysEnum) {
        return new Results(sysEnum);
    }

    public Results results(SysEnum sysEnum, T data) {
        return new Results(sysEnum, data);
    }

    public Results succeed() {
        return new Results(SysEnum.SUCCESS.getCode(), SysEnum.SUCCESS.getMsg());
    }

    public Results succeed(Object object) {
        Results results = succeed();
        results.setData(object);
        return results;
    }

    public Results succeed(String msg, Object data) {
        return new Results(SysEnum.SUCCESS.getCode(), msg, data);
    }

    public <T> Results succeed(PageInfo<T> page) {
        if (page == null) {
            page = new PageInfo<>();
        }
        return succeed(new SimplePage(page));
    }

    public Results fail(Integer code, String msg) {
        return new Results(code, msg);
    }

    public Results fail500() {
        return new Results(SysEnum.ERROR500.getCode(), SysEnum.ERROR500.getMsg());
    }


    public Results failData(T data) {
        return new Results(SysEnum.ERROR500.getCode(), msg, data);
    }

    public Results fail(SysEnum sysEnum) {
        return fail(sysEnum.getCode(), sysEnum.getMsg());
    }

    public Results fail(String msg) {
        return fail(SysEnum.ERROR500.getCode(), msg);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
        this.message = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
        this.msg = message;
    }

    public T getData() {
        return data;
    }

    public boolean isSuccess() {
        return SysEnum.SUCCESS.getCode().equals(this.getCode());
    }

    public void setData(T data) {
        this.data = data;
    }
}
